<?php
/*
Plugin Name: RhizomeCV
Plugin URI: http://code.google.com/p/rhizomecv/
Description: Compose your Europass CV and export views of it as HR-XML
Version: 1.6
Author: Rhizome Project (with assistance from University of Reading's Odinlab and EIfEL)
Author URI: http://code.google.com/p/rhizomecv/

This file is part of RhizomeCV. KiteCV updated through Rhizome project
Copyright ©2010 University of Reading, Odinlab (Karsten Oster Lundqvist) and EIfEL (Marc Van Coillie)

Extended from KiteCV
Copyright ©2006 Les Developpements Durables <contact@ldd.fr> and
Sébastien Ducoulombier <seb@ldd.fr>.

RhizomeCV is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

RhizomeCV is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// This file integrates KiteCVPlugin in Wordpress
// Updated to work in Wordpress 3.0.3

include_once 'code/KiteCVPlugin.php';

class KiteCV {
	var $dir;
	var $plugin;
	var $wp_plugin_dir;
	function KiteCV() {
		#$old_level = error_reporting(E_ALL);
			
		$this->wp_plugin_dir = WP_PLUGIN_URL;
		
		add_action('admin_head', array(&$this, 'start')); // load class when userdata is available in WP
		add_action('admin_head', array(&$this, 'add_head'));
		add_action('admin_menu', array(&$this, 'add_menu'));
		add_action('admin_notices', array(&$this, 'admin_notices'));
		add_filter('the_content', 'KiteCV_filter_content');
		load_plugin_textdomain('default', $this->wp_plugin_dir.'/RhizomeCV/locales');
		#error_reporting($old_level);
	}
	function start(){ // function to load KiteCV later, when WP userdata is available
		#$old_level = error_reporting(E_ALL);
		$this->dir = new KiteCVDir(
			dirname(__FILE__).'/data'
		);
		$this->plugin = new KiteCVPlugin(
			$this->dir,
			$this->wp_plugin_dir.'/RhizomeCV/requisites',							// HTML requisites URL
			$this->wp_plugin_dir.'/RhizomeCV/export.php?uid=%1$s&languageCode=%2$s&format=%3$s&position=%4$s',	// exportURL
			$this->wp_plugin_dir.'/RhizomeCV/export.php?downloadfile=%s',
			'wordpress'
		);
		//$objUser = wp_get_current_user();
		$userId = $this->dir->getSelectedMaster();//$objUser->ID;
		$userId = $this->plugin->setUserId($userId);
		
		if (array_key_exists('KiteCV_languageCode', $_POST)) {
			$KiteCV_languageCode = $_POST['KiteCV_languageCode'];
			$this->dir->setSelectedLanguage($userId, $KiteCV_languageCode);
		} else {
			$KiteCV_languageCode = $this->dir->getSelectedLanguage($userId);
		}
		if ($KiteCV_languageCode !== FALSE) {
			$this->plugin->setLanguageCode($KiteCV_languageCode);
			$this->plugin->openFile();
			if (array_key_exists('KiteCV_download', $_POST)) {
				$this->plugin->replyWithRawFile();
				exit;
			}
		}
		#error_reporting($old_level);
	}
	function add_head() {
		echo $this->plugin->getHeaderHTML();
	}
	function add_menu() {
		add_menu_page( "Europass CV", "Europass CV", 'manage_options', 'europass-top-level', array($this,'editor_page'));
		add_submenu_page( 'europass-top-level', "Edit CV", "Edit CV", 'manage_options', 'europass-top-level', array($this,'editor_page'));
		add_submenu_page( 'europass-top-level', "Import / Export CV", "Import / Export CV", 'manage_options', 'europass-manage', array($this,'management_page'));
		add_submenu_page( 'europass-top-level', "Help", "Help", 'manage_options', 'europass-help', array($this,'help_page'));
	}
	function admin_notices() {
		#$old_level = error_reporting(E_ALL);
		$this->plugin->doActions();
		if (FALSE !== ($errorMessage = $this->plugin->getErrorMessage())) {
 			echo '<div id="message" class="error fade"><p>'.$errorMessage.'</p></div>';
		} elseif (FALSE !== ($noticeMessage = $this->plugin->getNoticeMessage())) {
 			echo '<div id="message" class="updated fade"><p>'.$noticeMessage.'</p></div>';
		}
		#error_reporting($old_level);
	}
	
	function management_page() {
		if (!current_user_can('manage_options'))  {
			wp_die( __('You do not have sufficient permissions to access this page.') );
		}
		echo $this->plugin->getManagementDiv();
	}
	
	function editor_page() {
		if (!current_user_can('manage_options'))  {
			wp_die( __('You do not have sufficient permissions to access this page.') );
		}
		echo $this->plugin->getEditorDiv();
	}
	
	function help_page() {
		if (!current_user_can('manage_options'))  {
			wp_die( __('You do not have sufficient permissions to access this page.') );
		}
		echo $this->plugin->getHelp();
	}
	
	function control_page() {
		if (!current_user_can('manage_options'))  {
			wp_die( __('You do not have sufficient permissions to access this page.') );
		}
		echo $this->plugin->getControlDiv();
	}
	
	function getXHTMLView($languageCode, $position) {
		$this->plugin->setLanguageCode($languageCode);
		$this->plugin->openFile();
		return $this->plugin->getXHTMLView($position);
	}
}

$KiteCV = new KiteCV();

function KiteCV_filter_content($content) {
	echo preg_replace_callback(
		'/KiteCVView_(..)_([0-9]+)/',
		create_function(
			'$matches',
			'global $KiteCV;return $KiteCV->getXHTMLView($matches[1], $matches[2]);'
		),
		$content
	);
}
?>
